﻿using HedgehogTeam.EasyTouch;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.EventSystems;

public class LowLevelEventHandler : MonoBehaviour
{
    // Start is called before the first frame update
    void Start()
    {
        
    }

    const int LeftButtonIndex = 0;
    const int RightButtonIndex = 1;
    // Update is called once per frame
    void Update()
    {
        if (Input.GetMouseButtonDown(LeftButtonIndex))
        {
            RayCast(true);
        }
        if (Input.GetMouseButtonDown(RightButtonIndex))
        {
            RayCast(false);
        }
    }

    private void OnEnable()
    {
        EasyTouch.On_LongTapStart += OnLongTapStart;
        EasyTouch.On_Drag += OnDrag;
        EasyTouch.On_TouchUp += OnTouchUp;
    }

    private void OnDisable()
    {
        EasyTouch.On_LongTapStart -= OnLongTapStart;
        EasyTouch.On_Drag -= OnDrag;
        EasyTouch.On_TouchUp -= OnTouchUp;
    }

    private UnitBase _unitDrag;
    public void OnLongTapStart(Gesture gesture)
    {
        if (_unitDrag != null)
            return;

        Debug.Log("OnLongTapStart:" + gesture.position + " 获取离它最近的建筑，开始拖动。然后禁止拖动大地图");
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        var isHit = Physics.Raycast(ray, out hit, 1000, LayerMask.GetMask("Default"));
        if (isHit)
        {
            var city = CityMgr.Instance.GetNearCity(hit.point);
            if(city != null)
            {
                var unit = city.unitMgr.GetNearUnit(hit.point);
                if(unit != null)
                {
                    Debug.Log("点中建筑:" + unit);
                    _unitDrag = unit;
                    _unitDrag.SetIsDraging(true);

                    //禁止移动场景
                    Facade.Instance.SendEvent(GameEvent.UserInput.MoveUnit, true);//true开始移动unit
                }
            }

        }
       
    }

    public void OnDrag(Gesture gesture)
    {
        if (_unitDrag == null)
            return;
        //temp code:后面改成射线精准拖动
        var pos = _unitDrag.transform.localPosition;
        float factorX = 0.015f;
        float factorY = 0.023f;
        UtilGeometry.SetPosXY(ref pos, pos.x + gesture.deltaPosition.x * factorX, pos.PosY() + gesture.deltaPosition.y * factorY);

        _unitDrag.transform.localPosition = pos;
    }
    public void OnTouchUp(Gesture gesture)
    {
        Debug.Log("OnTouchUp");
        if (_unitDrag != null)
        {
            _unitDrag.SetIsDraging(false);
            _unitDrag = null;
            Facade.Instance.SendEvent(GameEvent.UserInput.MoveUnit, false);//true开始移动unit
        }
    }

    #region Temp code
    void RayCast(bool isLeftButton)
    {
        Ray ray = Camera.main.ScreenPointToRay(Input.mousePosition);
        RaycastHit hit;
        var isHit = Physics.Raycast(ray, out hit, 1000, LayerMask.GetMask("Default"));
        if (isHit)
        {
            int x;
            int y;
            HexMetrics.GetHexGridFromPos(hit.point, out x, out y);
            //MapMgr.Instance.Click(x, y);

            if (isLeftButton)
            {
                //获取起始点
                startMapItem = MapMgr.Instance.GetMapItem(x, y);
            }else {
                //获取终点
                endMapItem = MapMgr.Instance.GetMapItem(x, y);
            }

            //开始寻路
            if(startMapItem != null && endMapItem != null)
            {
                //ShowPath(); 寻路逻辑注释
            }
        }
    }

    public MapItem startMapItem;
    public MapItem endMapItem;
    private void ShowPath()
    {
        List<MapItem> mapItemList;
        AStarPath.ComputePath(startMapItem, endMapItem, MapMgr.Instance.GetMapItemList(), out mapItemList);

        //为了显示好看，加入起始点
        if (mapItemList.Count > 0)
            mapItemList.Insert(0, startMapItem);

        LinePath.Instance.ShowPath(mapItemList);
    }
    #endregion
}
